"""
Schedule schemas for request/response validation.
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional
from datetime import time, datetime
from uuid import UUID


class ScheduleBase(BaseModel):
    """Base schedule schema."""
    room_id: UUID
    day_of_week: int = Field(..., ge=0, le=6)  # 0=Monday, 6=Sunday
    open_time: time
    close_time: time
    is_available: bool = True
    
    @field_validator('close_time')
    @classmethod
    def validate_close_time(cls, v, info):
        """Validate that close_time is after open_time."""
        if 'open_time' in info.data and v <= info.data['open_time']:
            raise ValueError('close_time must be after open_time')
        return v


class ScheduleCreate(ScheduleBase):
    """Schema for creating a new schedule."""
    pass


class ScheduleUpdate(BaseModel):
    """Schema for updating schedule information."""
    day_of_week: Optional[int] = Field(None, ge=0, le=6)
    open_time: Optional[time] = None
    close_time: Optional[time] = None
    is_available: Optional[bool] = None


class ScheduleInDB(ScheduleBase):
    """Schema for schedule in database."""
    id: UUID
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


class ScheduleResponse(BaseModel):
    """Schema for schedule response."""
    id: UUID
    room_id: UUID
    day_of_week: int = Field(..., ge=0, le=6)
    open_time: time
    close_time: time
    is_available: bool
    room_name: Optional[str] = None
    created_at: datetime
    
    class Config:
        from_attributes = True
